One of the key challenges in wireless sensor networks is to design an energy efficient communication protocol. In this paper, we present an innovative code-based sleep and wake-up scheduling scheme to minimize energy consumption caused by idle listening. The proposed scheme called SWAP divides time into slots of equal length, and at the beginning of each slot a sensor node enters either an active or power-saving state. The slot scheduling at each node is based on a binary vector, which is constructed using the mathematical properties of finite fields. The set of scheduling vectors generated by SWAP distributes active slots of nodes evenly over entire time frame. This distribution of active slots reduces channel contention and allows better channel utilization. The SWAP scheduling scheme also ensures that any pair of neighboring nodes using the proposed scheduling scheme will be able to communicate each other since their active periods overlap at least once within a cycle of the sleep and wake-up slots. We also design a packet prioritization scheme in SWAP to reduce the packet latency of delay sensitive packets. To evaluate the energy efficiency and performance of the proposed mechanism, we have implemented SWAP in TOSSIM, and compared it with a widely used channel access protocol, BMAC with LPL, in terms of packet delivery ratio, power consumption and delay. The results of our study show that SWAP outperforms BMAC in terms of energy consumption while maintaining comparable packet delivery ratio and latency.